class ResumesController < ApplicationController
	
	before_filter :check_session,			# filter routine (applied before the action starts) checks for session
		:only => [:show, :new, :create, :edit, :update, :destroy, :show_p, :edit2] 	# actions where filter is applied

   	# GET /home
	def home
		# session check - 
		# filter is not applied here as it redirects to this action again, that causes redirection error
		if session[:current_user_id].nil?
			# user not logged in, redirect to root
			flash[:notice] = "No user logged in"
			render :file=>
				"../../public/index.html.erb"
			return	
		end
		# User is logged in, show the user's home page
		# @resumes = Resume.find(:all, :conditions => ["user_id =?", session[:current_user_id]])
		@user = User.find(session[:current_user_id])
		@resumes = @user.resumes.all(:order => "updated_at DESC")	
	end

	# GET /show/:title
	def show
		@user = User.find(session[:current_user_id])
		@resume = @user.resumes.find(:first, :conditions => ["title =?",params[:title]])
		@default_resume = @user.resumes.find(@user.default_resume)
		@rtemplate = Rtemplate.find(:first, :conditions => ["id =?",@resume.rtemplate_id])
		session[:return_to] = "/show/"+@resume.title
	end
	
	# GET /new
	def new
		@user = User.find(session[:current_user_id])
	end
	
	# POST /create
	def create
		@user = User.find(session[:current_user_id])
		@resume = @user.resumes.build(params[:resume])
		if @resume.save
			flash[:notice] = "New resume created"
			redirect_to '/show/'+@resume.title
		else	
			flash[:notice] = "Error: resume create"
			redirect_to root_url	
		end		
	end

	# GET /edit/:title
	def edit
		@user = User.find(session[:current_user_id])
		@resume = @user.resumes.find(:first, :conditions => ["title =?",params[:title]])	
	end
	
	# PUT /update/:title
	def update
		
		@user 	= User.find(session[:current_user_id])
		@resume = @user.resumes.find(:first, :conditions => ["title =?",params[:title]])	

		resumePut = params[:resume]

		resRetVal = @resume.update_attributes(resumePut)	# Update the name (resumePut contains the name only)
		if (params["ajax"]=="true" && params["flag"]=="name_only")
			if resRetVal
				render :json => { :retVal => "updated" }
			else
				render :json => { :retVal => "error creating" }				
			end
		end

		conRetVal = update_contact(@resume)					# Update the contact
		proRetVal = update_profile(@resume)					# Update the profile
		skiRetVal = update_skills(@resume)					# Update the skills
		eduRetVal = update_educations(@resume)				# Update the educations
		expRetVal = update_experiences(@resume)				# Update the experiences
		achRetVal = update_achievement(@resume)				# Update the achievement
		hobRetVal = update_hobby(@resume)					# Update the hobby

		errorText	=	""
		errorText 	+= 	resRetVal ? "Name updated; " 		: "Error updating name"   		
		errorText	+=	conRetVal ? "Contact updated; " 	: "Error updating contact" 		
		errorText	+=	proRetVal ? "Profile updated; " 	: "Error updating profile" 		
		errorText	+=	skiRetVal ? "Skills updated; " 		: "Error updating skills"   	
		errorText	+=	eduRetVal ? "Educations updated; "	: "Error updating educations"   
		errorText	+=	expRetVal ? "Experiences updated; " : "Error updating experiences"  
		errorText	+=	achRetVal ? "Achievements updated; ": "Error updating achievements" 
		errorText	+=	hobRetVal ? "Hobby updated; " 		: "Error updating hobby"   		
					
		flash[:notice] = errorText
		
		# Render the update template instead of redirecting. XXX - Redirection to be updated
		# redirect_to '/home'
	end

	# POST /toggle_public_status/:id
	def toggle_public_status
		@user = User.find(session[:current_user_id])
		@resume = @user.resumes.find(params[:resume_id])
		
		if params["request"] == "toggle"
			@resume.public_status = (@resume.public_status == "private") ? "public" : "private"
			result = @resume.save
			if result
				render :json => { :retVal => "true", "public_status" => @resume.public_status }
			else
				render :json => { :retVal => "error", "errorText" => result }
			end
		end
	end
	
	# DELETE /destroy/:title
	def destroy
		@user 	= User.find(session[:current_user_id])
		@resume = @user.resumes.find(:first, :conditions => ["title =?",params[:title]])	
		result 	= @resume.destroy	
		flash[:notice] = result ? "Resume destroyed" : "Error destroying resume"  
		if params["ajax"] == "true"
			if result
				render :json => { :retVal => "deleted" }
			else
				render :json => { :retVal => "error" }
			end
		else
			session[:return_to] = ""
			redirect_to root_url
		end
	end
	
	# GET /show_p/:title
	def show_p
		@user 	= User.find(session[:current_user_id])
		@resume = @user.resumes.find(:first, :conditions => ["title =?",params[:title]])
		@rtemplate = Rtemplate.find(@resume.rtemplate_id)
		render :file =>
			"../../public/" + @rtemplate.location			
	end
	
	# GET /edit2/:title
	def edit2
		@user = User.find(session[:current_user_id])
		@resume = @user.resumes.find(:first, :conditions => ["title =?",params[:title]])
		@skills = @resume.skills.all(:order => "skillset_type")
	end
	
end
